package development.app.mxh;

import state.Clip;
import state.GameConstants;
import state.GameForm;
import state.GameImage;
import state.Graphics;
import state.Key;
import state.Sprite;
import state.TouchScreenListener;
import state.component.Event;
import state.component.EventListener;
import state.component.GameScrollBar;
import state.font.FontManager;
import state.font.ImageText;
import state.socket.DataPackage;
import state.util.GameColor;
import state.util.GameStringUtils;
import state.util.Hashtable;
import state.util.ImageUtil;
import state.util.array.StringArray;
import development.Constants;
import development.DrawUtils;
import development.FrmUserInfo;
import development.Global;
import development.group.FrmGroupFriendAndTop;
import development.group.FrmGroupKetBanMangXHChat;
import development.socket.SocketClientUtil;

public class FrmAccount extends GameForm implements EventListener, TouchScreenListener {

  public static final int ACCOUNT_INFO_REQUEST = 801;
  public static final int ACCOUNT_INFO_RESPONSE = 802;

  private ImageText text8;
  private int dy;

  private final int DEFAULT_TEXT_X = 5;
  private GameScrollBar scrollBar;
  private Clip textView;

  private int accountFuctionIndex;
  private StringArray accountFuction;
  private String account;
  private GameImage accountAvatar;
  private String statusInfo;
  private Hashtable parameters1;
  private String currentUser;
  private static GameForm singleton;
  public static GameForm backForm;

  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmAccount();
    }
    return singleton;
  }

  public void init(Hashtable parameters) {
    this.parameters1 = parameters;
    if (parameters == null) {
      account = Global.currentUser.getName();
    } else {
      account = GameStringUtils.nvl(parameters.get("User"), Global.currentUser.getName());
    }
    accountFuction = new StringArray(new String[] { "Thông tin cá nhân", "Xem tường", "Xem album", "Bạn bè", "Kết bạn", "Gửi tin nhắn tới " + account },6);
    if(Global.currentUser.getName().equals(account)){
      accountFuction = new StringArray(new String[] { "Thông tin cá nhân", "Xem tường", "Xem album", "Bạn bè"},4);
    }
    accountFuctionIndex = 0;
    currentUser = Global.currentUser.getName();
    statusInfo = "";
    dy = DrawUtils.getTopBarHeight();
    text8 = FontManager.getFont(FontManager.FONT_SIZE_8);
    textView = new Clip(DEFAULT_TEXT_X, dy, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT - dy - 10);
    scrollBar = new GameScrollBar(Constants.SCREEN_WIDTH - 5, 25, 5, Constants.SCREEN_HEIGHT - 50, textView, _manager);
    accountAvatar = Global.getAvatar(account);
    accountAvatar = ImageUtil.resizeImage2(accountAvatar, 65, 65);
    DataPackage dataPackage = DataPackage.createDataPackage1(ACCOUNT_INFO_REQUEST);
    dataPackage.putString(account);
    Global.appClient.write(dataPackage);

    _manager.enableTouchPad();
  }

  public void draw(Graphics g) {
    DrawUtils.drawAppBackground(g);
    drawAccountFuction(g);
    DrawUtils.drawTopBar(g);
    text8.drawString2(g, "Tài khoản", GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, 0, GameConstants.TOP_HCENTER_ANCHOR);
    drawMenu(g);
  }

  private void drawAccountFuction(Graphics g) {
    int textY = dy;
    g.drawImage(accountAvatar, 0, textY, GameConstants.TOP_LEFT_ANCHOR);
    text8.setBold(true);
    text8.drawString3(g, account, GameColor.WHITE_CODE, accountAvatar.getWidth() + 5, textY, GameConstants.TOP_LEFT_ANCHOR, scrollBar);
    text8.setBold(false);
    textY = textY + 15;
    text8.drawParagraph2(g, statusInfo, GameColor.YELLOW_CODE, GameConstants.SCREEN_WIDTH - accountAvatar.getWidth(), accountAvatar.getWidth() + 5, textY, scrollBar);
    textY = accountAvatar.getHeight() + 30;
    for (int i = 0; i < accountFuction.len(); i++) {
      if (accountFuctionIndex == i) {
        g.setColor(0x53A4CF);
        g.fillRect(0, textY, GameConstants.SCREEN_WIDTH, 20);
      }
      text8.drawString3(g, accountFuction.array[i], GameColor.YELLOW_CODE, DEFAULT_TEXT_X, textY, GameConstants.TOP_LEFT_ANCHOR, scrollBar);
      textY = textY + 20;
      g.setColor(0xC0C0C0);
      g.drawLine(0, textY, GameConstants.SCREEN_WIDTH, textY);
    }
  }

  private void drawMenu(Graphics g) {
    DrawUtils.getBottomBar(_manager).setLabels("Menu MXH", "Chọn", "Trở về");
  }

  public void onRecieveData(DataPackage dataPackage) {
    int header = dataPackage.getHeader();
    switch (header) {
    case ACCOUNT_INFO_RESPONSE:
      statusInfo = dataPackage.nextString();
      break;
    }
  }

  public void keyReleased(int keyCode) {
    switch (keyCode) {
    case Key.SOFT_LEFT:
      Global.nextState(FrmGroupKetBanMangXHChat.instance(), parameters1);
      break;
    case Key.SOFT_RIGHT:
      if (backForm != null) {
        Global.nextState(backForm, null);
      } else {
        Global.nextState(FrmGroupKetBanMangXHChat.instance(), null);
      }
      break;
    case Key.DOWN:
      if (accountFuctionIndex < accountFuction.len() - 1) {
        accountFuctionIndex++;
      }
      break;
    case Key.UP:
      if (accountFuctionIndex > 0) {
        accountFuctionIndex--;
      }
      break;
    case Key.FIRE:
      // accountFuction = new String[] { "Thông tin cá nhân", "Xem tường", "Xem album", "Bạn bè","Kết bạn", "Gửi tin nhắn tới " + account };
      if (accountFuctionIndex == 0) {
        FrmUserInfo.goBackForm = this;
        Global.nextState(FrmUserInfo.instance(), parameters1);
      } else  if (accountFuctionIndex == 1) {
        if (currentUser.equals(account)) {
          parameters1.put("wallType", String.valueOf(FrmWall.MY_WALL_TYPE));
        } else {
          parameters1.put("wallType", String.valueOf(FrmWall.YOUR_WALL_TYPE));
        }
        FrmWall.backForm = this;
        Global.nextState(FrmWall.instance(), parameters1);
      } else if (accountFuctionIndex == 2) {
        if (currentUser.equals(account)) {
          parameters1.put("albumType", String.valueOf(FrmMyAlbum.MY_ALBUM_TYPE));
        } else {
          parameters1.put("albumType", String.valueOf(FrmMyAlbum.YOUR_ALBUM_TYPE));
        }
        FrmMyAlbum.backForm = this;
        Global.nextState(FrmMyAlbum.instance(), parameters1);
      } else if (accountFuctionIndex == 3) {
        FrmGroupFriendAndTop.backForm = this;
        Global.nextState(FrmGroupFriendAndTop.instance(), parameters1);
      } else if (accountFuctionIndex == 4) {
        SocketClientUtil.addFriendRequest(account);
      } else if (accountFuctionIndex == 5) {
        FrmSendMessage.backForm = this;
        Global.nextState(FrmSendMessage.instance(), parameters1);
      }
      break;
    }
  }

  public void onActionPerform(Event event) {
  }

  public void pointerDragged(Sprite source, int x, int y) {
  }

  public void pointerPressed(Sprite source, int x, int y) {
  }

  public void pointerReleased(Sprite source, int x, int y) {
  }

  public void destroy() {
    text8 = null;
    scrollBar = null;
    textView = null;

    accountFuctionIndex = 0;
    accountFuction = null;
    account = null;
    accountAvatar = null;
    statusInfo = null;
    parameters1 = null;
    currentUser = null;
  }
}
